package com.managertrade.dto.vo;

import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * 当前资金实体类
 * 用于记录账户总资金及相关信息
 */
public class AccountFunds {

    /** 主键ID，可选，用于数据库存储 */
    private Long id;

    /** 时间戳，记录资金时刻 */
    private LocalDateTime timestamp;

    /** 账户现金余额 */
    private BigDecimal cashBalance;

    /** 持仓市值 */
    private BigDecimal positionValue;

    /** 总资金 = 现金 + 持仓市值 */
    private BigDecimal totalFunds;

    /** 当前小时盈亏（可选） */
    private BigDecimal profitLoss;

    /** 可扩展字段，例如资金曲线平滑值 */
    private BigDecimal fundsEMA;

    // ---------- 构造方法 ----------
    public AccountFunds() {
    }

    public AccountFunds(LocalDateTime timestamp, BigDecimal cashBalance, BigDecimal positionValue) {
        this.timestamp = timestamp;
        this.cashBalance = cashBalance;
        this.positionValue = positionValue;
        this.totalFunds = cashBalance.add(positionValue);
    }

    // ---------- Getter & Setter ----------
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public LocalDateTime getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(LocalDateTime timestamp) {
        this.timestamp = timestamp;
    }

    public BigDecimal getCashBalance() {
        return cashBalance;
    }

    public void setCashBalance(BigDecimal cashBalance) {
        this.cashBalance = cashBalance;
        recalcTotalFunds();
    }

    public BigDecimal getPositionValue() {
        return positionValue;
    }

    public void setPositionValue(BigDecimal positionValue) {
        this.positionValue = positionValue;
        recalcTotalFunds();
    }

    public BigDecimal getTotalFunds() {
        return totalFunds;
    }

    public BigDecimal getProfitLoss() {
        return profitLoss;
    }

    public void setProfitLoss(BigDecimal profitLoss) {
        this.profitLoss = profitLoss;
    }

    public BigDecimal getFundsEMA() {
        return fundsEMA;
    }

    public void setFundsEMA(BigDecimal fundsEMA) {
        this.fundsEMA = fundsEMA;
    }

    // ---------- 计算方法 ----------
    private void recalcTotalFunds() {
        if (this.cashBalance != null && this.positionValue != null) {
            this.totalFunds = this.cashBalance.add(this.positionValue);
        }
    }

    @Override
    public String toString() {
        return "AccountFunds{" +
                "timestamp=" + timestamp +
                ", cashBalance=" + cashBalance +
                ", positionValue=" + positionValue +
                ", totalFunds=" + totalFunds +
                ", profitLoss=" + profitLoss +
                '}';
    }
}

